/*
 * 提供了自动生成运动员号码和赛道的方法
 */
package com.res0w.SMMS.fuction;

import com.res0w.SMMS.factory.DataBaseOperate;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author res0w
 * @since 2009-9-22
 * @version 0.1
 */
public class MakeId {

    public void autoMakeAthleteId() throws SQLException {
        ArrayList idList = new ArrayList();
        ArrayList ranNo = new ArrayList();
        DataBaseOperate db = new DataBaseOperate();
        String sql = "select distinct sId from signup_cachelist";
        try {
            idList = db.dbQuerySolo(sql, false);
            ranNo = randomNum(idList.size());

        } catch (SQLException ex) {
            Logger.getLogger(MakeId.class.getName()).log(Level.SEVERE, null, ex);
        }
        ArrayList rsList = new ArrayList();
        for (int i = 0; i < ranNo.size(); i++) {
            rsList.add("update signup_cachelist " +
                    "set athleteId='" + ranNo.get(i).toString().trim() + "' " +
                    "where sId='" + idList.get(i).toString().trim() + "'");
        }
        db.dbUpdateBatch(rsList);
    }

    /*
     * 此方法不完整,禁用
     * *CAUTION*
     */
    public void autoGroup(){
        DataBaseOperate db=new DataBaseOperate();
        ArrayList itemsList=new ArrayList();

        String sql = "select distinct joinItems from signup_cachelist";
        try {
            itemsList = db.dbQuerySolo(sql, false);
            for(int i=0;i<itemsList.size();i++){
                String items=itemsList.get(i).toString().trim();
            }
        } catch (SQLException ex) {
            Logger.getLogger(MakeId.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    /*
     *自动生成n个四位不相同随机数算法（已经格式化）
     */
    private ArrayList randomNum(int n) {
        Random random = new Random(88);
        ArrayList ranList = new ArrayList();
        int ran = random.nextInt(10000);
        ranList.add(ran);
        //
        for (int i = 1; i < n; i++) {
            boolean isRe = false;
            ran = random.nextInt(10000);
            for (int m = 0; m < i; m++) {
                if (ranList.get(m).toString().trim().equals(String.valueOf(ran))) {
                    i--;
                    isRe = true;
                    break;
                }
            }
            if (!isRe) {
                //进行格式转换
                ranList.add(formatList(ran));
            }
        }
        return ranList;
    }

    /*
     *格式化文档
     * @param ran 一个整数类型
     * @return 返回一个string类型值，并已进行格式化
     */
    private String formatList(int ran) {
        String newFormat = String.valueOf(ran);
        String[] taglib = {"000", "00", "0", ""};//自动补零算法
        newFormat = taglib[newFormat.length() - 1] + newFormat;
        return newFormat;
    }
}
